package day06Case02;

import day06Case01.Print_Array;

/**
 * @author Xiao
 * @date 2021/7/6
 */
public class Reverse_Array {
    /**
     * 需求：
     * 1.将arr数组中的元素反转
     * 2.反转后的数组中索引为奇数的元素互换，1和3换，3和5换……
     * 最后一个索引为奇数的元素和第一个索引为奇数的元素换
     * 打印最终的数组
     */
    public static void main(String[] args) {
        int[] arr = Print_Array.getAnyArray();
//        int[] arr = {1, 2, 3, 4, 5, 6};
        System.out.println("随机int数组如下：");
        Print_Array.printArray(arr);
        System.out.println("-------------");
        int[] arr1 = reverseArray(arr);
        System.out.println("反转后的数组如下：");
        Print_Array.printArray(arr1);
        System.out.println("-------------");
        int[] arr2 = changeEvenNum(arr1);
        System.out.println("交换奇数后数组如下：");
        Print_Array.printArray(arr2);
/*
        int[] arr3 = {1,2,3,4,5,6,7};
        int[] arr4 = {1,2,3,4,5,6,7,8};
        arr3 = changeEvenNum(arr3);
        arr4 = changeEvenNum(arr4);
        Print_Array.printArray(arr3);
        Print_Array.printArray(arr4);
*/
    }

    public static int[] changeEvenNum(int[] arr) {
        if (arr == null) {
            return null;
        }
/*
        if (arr.length % 2 == 0) {
            int start = 1;
            int end = arr.length - 1;
            for (; start < end; ) {
                int temp = arr[start];
                arr[start] = arr[start + 2];
                arr[start+2] = temp;
                start += 2;
            }
            int temp = arr[end];
            arr[end] = arr[1];
            arr[1] = temp;
        } else {
            int start = 1;
            int end = arr.length - 2;
            for (; start < end; ) {
                int temp = arr[start];
                arr[start] = arr[start + 2];
                arr[start+2] = temp;
                start += 2;
            }
            int temp = arr[end];
            arr[end] = arr[1];
            arr[1] = temp;
*/
        for (int i = 1; i < arr.length - 2; i += 2) {
            int temp = arr[i];
            arr[i] = arr[i + 2];
            arr[i + 2] = temp;
        }
        if (arr.length % 2 == 0) {
            int temp = arr[1];
            arr[1] = arr[arr.length - 1];
            arr[arr.length - 1] = temp;
        } else {
            int temp = arr[1];
            arr[1] = arr[arr.length - 2];
            arr[arr.length - 2] = temp;
        }
        return arr;
    }

    public static int[] reverseArray(int[] arr) {
        int start = 0;
        int end = arr.length - 1;
        for (; start < end; ) {
            int temp = arr[start];
            arr[start] = arr[end];
            arr[end] = temp;
            start++;
            end--;
        }
        return arr;
    }
}
